package cn.qyl.joha.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.qyl.joha.common.PageConstant;
import cn.qyl.joha.utils.AuthCode;






@WebServlet("/tocode")
public class CodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String randomCode =  AuthCode.getRandomNum();
		
		request.getSession().setAttribute(PageConstant.AUTH_CODE, randomCode);
		
		BufferedImage image =  AuthCode.getAuthCodeImg(randomCode);
		
		ImageIO.write(image, "jpg", response.getOutputStream());
	}

}
